1225C - p-binary - CodeForces Solution


bitmasks brute force math *1600

Please click on ads to support us..

Python Code:

n, p = map(int, input().split(' '))

def main(n, p):
    for i in range(1, 65):
        if (n - i*p > 0):
            s = bin(n- i*p)[2:]
            sum = 0
            for j in range(len(s)):
                if(s[len(s) -1 -j] == '1'):
                    sum += j+1
            if(sum >= i and s.count('1') <= i):
                                                            return i
                            
        
    return -1

print(main(n, p))

C++ Code:

            ///~~~LOTA~~~///
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
#define li long int
#define ld long double
#define append push_back
#define add insert
#define nl "\n"
#define ff first
#define ss second
#define pii pair<int,int>
#define pic pair<int,char>
#define all(x) (x).begin(),(x).end()
#define sum(a) accumulate(all(a),0)
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL)
#define terminator main
#define MAXN 1001
void solve(){
    int n,m,o,p,q;
    cin>>n>>m;
    o=1;
    while(o*m<n){
        if(__builtin_popcount(n-o*m)<=o && (n-o*m)>=o){
            cout<<o;
            return;
        }
        o++;
    }
    cout<<-1;
}
int terminator(){
    L0TA;
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array